﻿using UnityEngine;
using System;
using System.Collections;

namespace Baiyy {
	public class HashtableEx : IEnumerable {
		#region vars
		protected Hashtable _data = new Hashtable();
		#endregion

		#region getter&setter
		public Hashtable data { 
			get { return _data; } 
			set { 
				if (value == null) {
					throw new ArgumentNullException ();
				}
				_data = value; 
			}
		}

		public int Count {
			get { return _data.Count; }
		}

		public object this[object key] {
			get { return _data[key]; }
			set { _data [key] = value; }
		}
		#endregion

		#region methods
		public HashtableEx(Hashtable data = null) {
			if (data != null) {
				_data = data;
			}
		}

		public HashtableEx(HashtableEx data) {
			_data = data.data;
		}

		public IEnumerator GetEnumerator() {
			return _data.GetEnumerator ();
		}

		public bool Contains(object key) {
			return _data.Contains (key);
		}

		public bool ContainsKey(object key) {
			return _data.ContainsKey (key);
		}

		public bool ContainsValue(object value) {
			return _data.ContainsValue (value);
		}

		public void Add(object key, object value) {
			_data.Add (key, value);
		}

		public void AddData(Hashtable data) {
			foreach (DictionaryEntry de in data) {
				_data [de.Key] = de.Value;
			}
		}

		public void Remove(object key) {
			_data.Remove (key);
		}

		public void Clear() {
			_data.Clear ();
		}
		#endregion

		#region Converter
		public bool asBoolean(object key) {
			return Convert.ToBoolean (_data [key]);
		}
		public byte asByte(object key) {
			return Convert.ToByte (_data [key]);
		}
		public char asChar(object key) {
			return Convert.ToChar (_data [key]);
		}
		public int asInt(object key) {
			return Convert.ToInt32 (_data [key]);
		}
		public string asString(object key) {
			return Convert.ToString (_data [key]);
		}
		public ArrayList asArray(object key) {
			return (ArrayList)_data[key];
		}
		#endregion
	}
}
